﻿Ext.define('app.prm.document.DocumentController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.document',
    init: function () {
        var me = this;
    },
    onInit: function (view) {
        App.AjaxGet('/api/services/app/permission/GetHasSetType?projectId=' + view.projectId, function (data) {
            view.getViewModel().set('hasSetType', data.result.hasSetType);
            view.getViewModel().set('hasTrash', data.result.hasTrash);
        });

        var typeId = Ext.util.Cookies.get("projectdocumentreport" + view.projectId);
        if (typeId) {
            view.getGrid().store.load({ params: { typeId: typeId, projectId: view.projectId, isDelete: false } });
            Ext.util.Cookies.clear("projectdocumentreport" + view.projectId);
        }
    },
    onTreeSelect: function (tree, record, index, eOpts) {
        var view = this.getView();


        if (record.data.leaf || record.data.id == -1) {

            var id = record.get('id');
            var stepname = "";

            var __index = id.indexOf('_');
            var __length = id.length;
            if (__index > 0) {
                stepname = id.substring(__index + 1);
                id = id.substring(0, __index);
            }
            console.log(id + '>>' + stepname);
            App.AjaxGet('/api/services/app/permission/GetHasAuth?typeId=' + id + "&projectId=" + view.projectId, function (data) {
                view.getViewModel().set('hasUpload', !data.result.hasUpload);
                view.getViewModel().set('hasDelete', !data.result.hasDelete);
                view.getViewModel().set('hasDownload', !data.result.hasDownload);
                view.getViewModel().set('multipleFiles', data.result.multipleFiles);
                //view.getViewModel().set('hasTrash', !data.result.hasTrash);
            });


            var typeId = id;
            if (record.data.id == -1) {
                typeId = -1;
            }
            view.getGrid().store.filter = { typeId: typeId, stepname: stepname, projectId: view.projectId, isDelete: false };
            view.gridbbar.moveFirst();
        } else {
            view.getViewModel().set('hasUpload', true);
            view.getViewModel().set('hasDelete', true);
            view.getViewModel().set('hasDownload', true);
            view.getViewModel().set('multipleFiles', true);
        }

    },
    onOpenTrash: function () {
        var view = this.getView();
        this.redirectTo('app.prm.document.Trash/' + view.projectId);
    }
});
